FROM --platform=$BUILDPLATFORM node:18.14-alpine as build
RUN apk add --no-cache libc6-compat
RUN apk update

RUN npm install turbo --global

WORKDIR /app
ENV PATH=/app/node_modules/.bin:$PATH

COPY package.json ./
COPY package-lock.json ./
COPY ./frontend-sdk/package.json ./frontend-sdk/package.json
COPY ./elements/package.json ./elements/package.json

RUN npm ci --silent

COPY . .
RUN npm run build:elements

FROM nginx:stable-alpine
COPY --from=build /app/elements/dist/elements.js /usr/share/nginx/html
COPY --from=build /app/frontend-sdk/dist/sdk.* /usr/share/nginx/html

COPY elements/nginx/default.conf /etc/nginx/conf.d/default.conf
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]
